<?php
include "../include/global.php";
include "../include/admin_check.php";
$xtpl = new XTemplate ("hotel_tour_similar.htm");
include "setting.php";
//
include "../include/function.php";
$xtpl->assign("left_panel", left_panel());
$xtpl->assign("header_panel", header_panel());
$xtpl->assign("footer_panel", footer_panel());

// ++ lang
if($lang=='v'){
	$xtpl->assign('type_vietnamese', 'onkeyup="initTyper(this)"');
}
if($_GET['type']=='tour'){
	$xtpl->assign("title", TourExtend);
}else{
	$xtpl->assign("title", HotelSimilar);
}
$xtpl->assign("lbl_search", Search);
$xtpl->assign("lbl_no", No);
$xtpl->assign("lbl_name", Name);
$xtpl->assign("lbl_hotel", Gallery);
$xtpl->assign("lbl_view", View);
$xtpl->assign("lbl_position", Position);
$xtpl->assign("lbl_active", Active);
$xtpl->assign("lbl_update", Update);
$xtpl->assign("lbl_edit", Edit);
$xtpl->assign("lbl_delete", Delete);
$xtpl->assign("lbl_addnew", Addnew);
$xtpl->assign("lbl_total", Total);
$xtpl->assign("lbl_row", Row);
$xtpl->assign("lbl_close", Close);
// -- lang
//
$hotel_id = intval($_GET['hotel_id']);
$xtpl->assign("hotel_id", $hotel_id);

$type = _POST('type');
$xtpl->assign("type", $type);
if($type=='tour'){
	$field = 'hotel_tour';
	$sql = "SELECT $field FROM ".pre_table."hotel WHERE hotel_id='".$hotel_id."'";
	$rs = recordset($sql);
	$in_tour_similar = " AND tour_id IN ($rs[$field])"; 
	$notin_tour_similar = " AND tour_id NOT IN ($rs[$field])"; 

}else{
	$field = 'hotel_similar';
	$sql = "SELECT $field FROM ".pre_table."hotel WHERE hotel_id='".$hotel_id."'";
	$rs = recordset($sql);
	$in_tour_similar = " AND hotel_id IN ($rs[$field])"; 
	$notin_tour_similar = " AND hotel_id NOT IN ($rs[$field])"; 
}


//

$hiddenID=$_POST['hiddenID'];
$chkIsActive=$_POST['chkIsActive'];


if($_POST['ac']=="update"){
	$hotel_hotel_similar = implode(",", $chkIsActive);
	echo $sql = "UPDATE ".pre_table."hotel SET ".$field." = CONCAT(".$field.", ',".$hotel_hotel_similar."') WHERE hotel_id = '".$hotel_id."'";
	$rs = execSQL($sql);
	
	echo '<script>
				window.location.href="'.$_SERVER['REQUEST_URI'].'";
			</script>';
}
if($_GET['ac']=="delete"){
	$id = intval($_GET['id']);
	$sql = "UPDATE ".pre_table."hotel SET ".$field." = REPLACE(".$field.", ',".$id."', '') WHERE hotel_id = '".$hotel_id."'";
	$rs = execSQL($sql);
	echo '<script>
				window.location.href="'.$_SERVER['HTTP_REFERER'].'";
			</script>';
}

//
$condition="";
$condition.=" AND lang='".$lang."'";

// search
$key=$_GET['key'];
if($key){
	if($type=='tour'){
		$notin_tour_similar.=" AND (c.tour_name like '%$key%' OR c.tour_brief like '%$key%')";
		
		$xtpl->assign("key",$key);
		
		$sql="SELECT c.tour_id AS id, c.tour_name AS name FROM ".pre_table."tour c WHERE 1=1".$condition.$notin_tour_similar." 
				order by tour_position asc";
	}else{
		$notin_tour_similar.=" AND (c.hotel_name like '%$key%' OR c.hotel_brief like '%$key%') AND hotel_id<>'".$hotel_id."'";
		
		$xtpl->assign("key",$key);
		
		$sql="SELECT c.hotel_id AS id, c.hotel_name AS name FROM ".pre_table."hotel c WHERE 1=1".$condition.$notin_tour_similar." 
				order by hotel_position asc";
	}
		
	$rs = execSQL($sql);
	$numrec=mysql_num_rows($rs);
	$OrderNo=$numrec+1;
	
	while($row = mysql_fetch_array($rs)){
		
		$OrderNo=--$OrderNo;
		$row[OrderNo] = $OrderNo;
		
		$row['name'] = $row['name'];
		$row['id'] = $row['id'];
	
		$xtpl->insert_loop("MAIN.list",array("list"=>$row));	
	}
	
	$xtpl->assign("numrec",$numrec);
	
}
// hotel hotel and similar
if($type=='tour'){
	$sql2="SELECT c.tour_id AS id, c.tour_name AS name FROM ".pre_table."tour c WHERE 1=1".$condition.$in_tour_similar." 
		order by tour_position asc";
}else{
	$sql2="SELECT c.hotel_id AS id, c.hotel_name AS name FROM ".pre_table."hotel c WHERE 1=1".$condition.$in_tour_similar." 
		order by hotel_position asc";
}
$rs2 = execSQL($sql2);
$numrec2=mysql_num_rows($rs2);
//$OrderNo2=$numrec2+1;
$no=1;
while($row2 = mysql_fetch_array($rs2)){
    
	$OrderNo2=$no++;
	$row2[OrderNo2] = $OrderNo2;

	$row2['name'] = $row2['name'];
	$row2['id'] = $row2['id'];
	
    $xtpl->insert_loop("MAIN.list2",array("list2"=>$row2));	
}

$xtpl->assign("numrec2",$numrec2);

//
$xtpl->parse("MAIN");
eval("?".">".$xtpl->text("MAIN"));
?>